package com.xteam.utils;

import java.util.HashMap;
import java.util.Map;

public final class CommonMsg {

	public static final Map<String, Object> createDefaultMessage() {
		return new HashMap<String, Object>();
	}

	/**
	 * 信息添加成功消息
	 * @return
	 * @author yuanjie.wu
	 */
	public static final Map<String, Object> createAddSuccessMessage() {
		return createMessageMap(Type.ADD_SUCCESS, true);
	}

	/**
	 * 信息添加失败消息
	 * @return
	 * @author yuanjie.wu
	 */
	public static final Map<String, Object> createAddFailureMessage() {
		return createMessageMap(Type.ADD_FAILURE, false);
	}

	public static final Map<String, Object> createUpdateSuccessMessage() {
		return createMessageMap(Type.UPDATE_SUCCESS, true);
	}

	public static final Map<String, Object> createUpdateFailureMessage() {
		return createMessageMap(Type.UPDATE_FAILURE, false);
	}

	public static final Map<String, Object> createDeleteSuccessMessage() {
		return createMessageMap(Type.DELETE_SUCCESS, true);
	}

	public static final Map<String, Object> createDeleteFailureMessage() {
		return createMessageMap(Type.DELETE_FAILURE, false);
	}

	/**
	 * 数据库错误
	 * @return
	 * @author yuanjie.wu
	 */
	public static final Map<String, Object> createDBErrorMessage() {
		return createMessageMap(Type.DATABASE_ERROR, false);
	}

	enum Type {
		ADD_SUCCESS("信息保存成功！"), 
		ADD_FAILURE("信息保存失败！"), 
		UPDATE_SUCCESS("信息更新成功！"), 
		UPDATE_FAILURE("信息更新失败！"), 
		DELETE_SUCCESS("信息删除成功！"), 
		DELETE_FAILURE("信息删除失败！"), 
		DATABASE_ERROR("数据库失败！");

		final String text;

		Type(String text) {
			this.text = text;
		}
	}

	private static final Map<String, Object> createMessageMap(Type type, boolean op) {
		Map<String, Object> message = new HashMap<String, Object>();
		message.put("state", op);
		message.put("msg", type.text);
		return message;
	}
}
